package cn.kgc.springboot.controller;

import cn.kgc.springboot.entity.Permission;
import cn.kgc.springboot.service.PermissionService;
import cn.kgc.springboot.util.CodeMsg;
import cn.kgc.springboot.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: hengge
 * @version: v1.0
 * @DATE: 2022/11/28
 */
@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired(required = false)
    private PermissionService permissionService;

    //1.查询权限表数据(permission表)
    @RequestMapping("list")
     public ResponseResult permissionList(){

         ResponseResult responseResult = new ResponseResult();
         List<Permission> list = permissionService.list();
         responseResult.setData(list);
         responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
         responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());

         return responseResult;
    }

    //2.查询菜单和目录
    @RequestMapping("treeSelect")
    public ResponseResult treeSelect(){
        ResponseResult responseResult = permissionService.treeSelect();
        return responseResult;
    }

    //3.添加操作
    @RequestMapping("add")
    public ResponseResult add(Permission permission){
        //System.out.println("permission = " + permission);

        ResponseResult responseResult = new ResponseResult();
        permissionService.save(permission);
        responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
        responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
        return responseResult;
    }

    //4.编辑操作中的回显操作
    //根据权限id，查询权限的具体信息，回显在表单中
    @RequestMapping("selectById")
    public ResponseResult selectById(Integer id){
        System.out.println("id = " + id);
        Permission permission = permissionService.getById(id);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
        responseResult.setData(permission);
        return responseResult;
    }

    //5.编辑操作
    @RequestMapping("update")
    public ResponseResult update(Permission permission){
        permissionService.updateById(permission);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
        return responseResult;
    }

    //6.删除操作
    @RequestMapping("delete")
    public ResponseResult delete(Integer id){
        ResponseResult responseResult = permissionService.deleteById(id);
        return responseResult;
    }

}
